<template>
    <div class="commentBox">
        <!-- 头部 -->
        <van-nav-bar title="精彩评论" left-arrow
        @click-left="$router.push('/articleDetail?id='+$route.query.id)">
        </van-nav-bar>
        <!-- 评论列表 -->
        <div class="item" v-for="item in commentList" :key="item.id">
            <div class="head">
                <img :src="item.user.head_img|joinPath" alt />
                <div>
                    <p>{{item.user.nickname}}</p>
                    <span>{{item.create_date}}</span>
                </div>
                <span @click="replyFn(item.id)">回复</span>
            </div>
            <!-- 嵌套评论 star -->
            <commentItem v-if="item.parent" 
            :obj="item.parent" @reply="replyFn"></commentItem>
            <!-- 嵌套评论 end -->
            <div class="text">{{item.content}}</div>
        </div>
        <!-- 底部 -->
        <footerComment ref="footerComment" @updateList="updateListFn"></footerComment>
    </div>
</template>

<script>
import { post_comment }  from "@/api/article.js"
import footerComment from "@/components/footerComment.vue"
import commentItem from "@/components/commentItem.vue"
export default {
    components:{footerComment, commentItem},
    data(){
        return {
            commentList: []
        }
    },
    created(){
        // post_comment(this.$route.query.id).then(res=>{
        //     // console.log(32,res);
        //     this.commentList = res.data.data;
        // })
        this.updateListFn()
    },
    methods: {
        // 获取评论列表数据
        updateListFn(){
            post_comment(this.$route.query.id).then(res=>{
                // console.log(32,res);
                this.commentList = res.data.data;
            })
        },
        // 回复
        replyFn(id){
            // console.log(this.$refs.footerComment);
            // 控制底部文本域的显示
            this.$refs.footerComment.showTextarea()
            // console.log(59,id);
            // 将回复的id传递给子组件
            this.$refs.footerComment.parent_id = id;
        }
    }
}
</script>

<style lang="less" scoped>
/deep/.van-nav-bar{
    border-bottom: 2px solid #e9e9e9;
    .van-nav-bar__arrow,.van-icon{
        color: #000;
    }
}
.commentBox{
    padding-bottom: 52px;
    .item {
    padding: 10px;
    border-bottom: 1px solid #ccc;
    .head {
        display: flex;
        justify-content: space-between;
        align-items: center;
        > img {
            width: (50/360) * 100vw;
            height: (50/360) * 100vw;
            display: block;
            border-radius: 50%;
        }
        > div {
            flex: 1;
            display: flex;
            flex-direction: column;
            margin-left: 10px;
            > span {
                font-size: 12px;
                color: #999;
                line-height: 25px;
            }
        }
        > span {
            color: #999;
            font-size: 13px;
        }
    }
    .text {
        font-size: 14px;
        color: #333;
        padding: 20px 0 10px 0;
    }
}
}
</style>